package im.oen.gui.tools.window;

import im.oen.boot.common.utils.Checker;
import im.oen.gui.tools.cache.ConnectCache;
import im.oen.gui.tools.component.JTreeNode;
import im.oen.gui.tools.data.connect.AbsConnect;
import im.oen.gui.tools.util.MsgUtil;
import im.oen.gui.tools.util.SwingUtil;
import im.oen.gui.tools.window.model.MysqlConnect;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.util.Collection;

/**
 * @author Kevin.Xu
 */
public class MainFrame extends JFrame {

    private JTreeNode root;
    private JPanel content;
    private JPanel state;

    public MainFrame() {
        super();
        this.init();
    }

    private void init() {
        this.initInfo();
        this.initStyle();
        this.initMenus();
        this.initLayout();
    }

    private void initStyle() {
        UIManager.put("Tree.collapsedIcon", null);
        UIManager.put("Tree.expandedIcon", null);
    }

    private void initInfo() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setTitle(MsgUtil.get("title.main"));
        this.setSize(1024, 768);
        this.setBackground(Color.WHITE);
        this.getContentPane().setBackground(Color.WHITE);
        this.setResizable(true);
        this.setLocationRelativeTo(null);
    }

    private void initMenus() {
        JMenuBar menuBar = new JMenuBar();

        JMenu mainMenu = new JMenu(MsgUtil.get("menu.main"));
        JMenuItem mainAbout = new JMenuItem(MsgUtil.get("menu.main.about"));
        mainAbout.addActionListener((evt) -> {
            System.out.println("主菜单=>关于");
        });
        mainMenu.add(mainAbout);

        JMenu connectMenu = new JMenu(MsgUtil.get("menu.connect"));
        JMenuItem connectMysql = new JMenuItem(MsgUtil.get("menu.connect.mysql"));
        connectMysql.addActionListener((evt) -> {
            new MysqlConnect(MainFrame.this).setVisible(true);
        });
        JMenuItem connectMongo = new JMenuItem(MsgUtil.get("menu.connect.mongo"));
        JMenuItem connectRedis = new JMenuItem(MsgUtil.get("menu.connect.redis"));

        connectMenu.add(connectMysql);
        connectMenu.add(connectMongo);
        connectMenu.addSeparator();
        connectMenu.add(connectRedis);

        menuBar.add(mainMenu);
        menuBar.add(connectMenu);
        this.setJMenuBar(menuBar);
    }

    private void initMenus1() {
        JMenuBar menuBar = new JMenuBar();

        JMenu mainMenu = new JMenu(MsgUtil.get("menu.main"));
        JMenuItem mainAbout = new JMenuItem(MsgUtil.get("menu.main.about"));
        mainAbout.addActionListener((evt) -> {
            System.out.println("主菜单=>关于");
        });
        mainMenu.add(mainAbout);


        JMenu fileMenu = new JMenu("文件");

        JMenuItem newMenuItem = new JMenuItem("新建");

        /*
         * 菜单项的点击/状态改变事件监听，事件监听可以直接设置在具体的子菜单上（这里只设置其中几个）
         */
        // 设置 "新建" 子菜单被点击的监听器
        newMenuItem.addActionListener((evt) -> {
            System.out.println("新建  被点击");
        });


        JMenuItem openMenuItem = new JMenuItem("打开");
        JMenuItem exitMenuItem = new JMenuItem("退出");

        // 子菜单添加到一级菜单
        fileMenu.add(newMenuItem);
        fileMenu.add(openMenuItem);
        // 添加一条分割线
        fileMenu.addSeparator();
        fileMenu.add(exitMenuItem);

        JMenu editMenu = new JMenu("编辑");

        JMenuItem copyMenuItem = new JMenuItem("复制");
        JMenuItem pasteMenuItem = new JMenuItem("粘贴");
        // 子菜单添加到一级菜单
        editMenu.add(copyMenuItem);
        editMenu.add(pasteMenuItem);

        JMenu viewMenu = new JMenu("视图");
        final JCheckBoxMenuItem checkBoxMenuItem = new JCheckBoxMenuItem("复选框子菜单");
        final JRadioButtonMenuItem radioButtonMenuItem01 = new JRadioButtonMenuItem("单选按钮子菜单01");
        final JRadioButtonMenuItem radioButtonMenuItem02 = new JRadioButtonMenuItem("单选按钮子菜单02");
        // 子菜单添加到一级菜单
        viewMenu.add(checkBoxMenuItem);
        // 添加一个分割线
        viewMenu.addSeparator();
        viewMenu.add(radioButtonMenuItem01);
        viewMenu.add(radioButtonMenuItem02);


        // 其中两个 单选按钮子菜单，要实现单选按钮的效果，需要将它们放到一个按钮组中
        ButtonGroup btnGroup = new ButtonGroup();
        btnGroup.add(radioButtonMenuItem01);
        btnGroup.add(radioButtonMenuItem02);

        // 默认第一个单选按钮子菜单选中
        radioButtonMenuItem01.setSelected(true);

        JMenu aboutMenu = new JMenu("关于");

        menuBar.add(mainMenu);
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(viewMenu);
        menuBar.add(aboutMenu);

        this.setJMenuBar(menuBar);
    }

    private void initLayout() {
        this.setLayout(new BorderLayout());

        Border etchedBorder = BorderFactory.createEtchedBorder();
        JPanel westPanel = SwingUtil.createPanel();
        westPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        westPanel.setPreferredSize(new Dimension(300, 730));
        westPanel.setBorder(etchedBorder);

        this.root = new JTreeNode("数据连接");
        JTree tree = new JTree(root);
        tree.addTreeSelectionListener((evt) -> {
            JTreeNode node = (JTreeNode) tree.getLastSelectedPathComponent();
            if (Checker.isEmpty(node)) {
                return;
            }
            boolean leaf = node.isLeaf();
            if (!leaf) {
                return;
            }

            Object data = node.getObj();

            while (node != null && !node.isAbsConnect()) {
                node = node.getParentNode();
            }

            if (Checker.isEmpty(node) || !node.isAbsConnect()) {
                return;
            }

            AbsConnect connect = node.getData();
            MainFrame.this.showRightPanel(connect, data);
        });
        westPanel.add(tree);

        this.content = SwingUtil.createPanel();
        content.setPreferredSize(new Dimension(724, 730));
        content.setBorder(etchedBorder);
        content.add(SwingUtil.createLabel("CENTER"));

        this.state = SwingUtil.createPanel();
        state.setLayout(new FlowLayout(FlowLayout.RIGHT));
        state.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(4, 4, 4, 4)));
        state.setPreferredSize(new Dimension(1024, 30));
        state.setBorder(etchedBorder);
        state.add(SwingUtil.createLabel("State"));

        this.add(westPanel, BorderLayout.WEST);
        this.add(content, BorderLayout.CENTER);
        this.add(state, BorderLayout.SOUTH);
    }

    public void showRightPanel(AbsConnect data, Object nodeData) {

    }

    public void syncConnect() {
        Collection<AbsConnect> list = ConnectCache.all();
        if (Checker.isEmpty(list)) {
            return;
        }
        int count = this.root.getChildCount();
        for (int i = 0; i < count; i++) {
            this.root.remove(i);
        }

        for (AbsConnect connect : list) {
            JTreeNode node = new JTreeNode(connect);
            node.add(new JTreeNode("tables"));
            this.root.add(node);
        }
    }
}
